package ny.com.demo517;

import java.util.HashSet;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;

public class TestSet {
    // 定义Emp类
    static class Emp {
        private String id;
        private String name;
        private int age;

        public Emp(String id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        // Getter方法
        public String getName() {
            return name;
        }

        // 重写toString方法
        @Override
        public String toString() {
            return "Emp{id='" + id + "', name='" + name + "', age=" + age + '}';
        }

        // 重写equals和hashCode方法
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Emp emp = (Emp) o;
            return age == emp.age &&
                    Objects.equals(id, emp.id) &&
                    Objects.equals(name, emp.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, name, age);
        }
    }

    public static void main(String[] args) {
        Set<Emp> empSet = new HashSet<>();
        // 添加元素
        Emp emp1 = new Emp("1001", "小乔", 18);
        Emp emp2 = new Emp("1002", "大乔", 18);

        empSet.add(emp1);
        empSet.add(emp2);
        System.out.println("=====所有员工信息=====");
        for (Emp emp : empSet) {
            System.out.println(emp);
        }
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要查询的员工姓名：");
        String empName = scanner.next();
        boolean found = false;
        System.out.println("=====查询结果=====");

        // 查询员工信息
        for (Emp emp : empSet) {
            if (empName.equals(emp.getName())) {
                System.out.println(emp);
                found = true;
            }
        }

        if (!found) {
            System.out.println("未找到姓名为\"" + empName + "\"的员工");
        }
        scanner.close();
    }
}